package com.masterclient.app.modules.goods;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.masterclient.app.R;
import com.eoner.baselibrary.bean.goods.GoodDetailBean;
import com.eoner.baselibrary.bean.goods.PromotionBean;
import com.masterclient.app.utils.CommonUtil;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;

import java.util.List;


/**
 * 作者：sean
 * 创建时间：2017/9/5 10:18
 * 功能说明：
 */

public class CouponDialog extends Dialog {

    private Context mContext;
    private String title;
    private CouponAdapter couponAdapter;
    List<GoodDetailBean.DataBean.ShSalesrulesBean> list;
    List<PromotionBean> promotionList;
    private String desc;

    public CouponDialog(@NonNull Context context, String title, List<GoodDetailBean.DataBean.ShSalesrulesBean> list
            , List<PromotionBean> promotionList,String desc) {
        super(context, R.style.MyDialog);
        this.list = list;
        this.promotionList = promotionList;
        this.desc = desc;
        mContext = context;
        this.title = title;
    }

    public void notifyDataChange(List<GoodDetailBean.DataBean.ShSalesrulesBean> list, List<PromotionBean> promotionList
            ,String desc) {
        this.list = list;
        this.promotionList = promotionList;
        this.desc = desc;
        couponAdapter.setNewData(list);
        couponAdapter.notifyDataSetChanged();
    }

    private TextView tvTitle;
    private TextView tvCancel;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_coupon);
        setCanceledOnTouchOutside(true);
        tvTitle = findViewById(R.id.tv_title);
        tvCancel = findViewById(R.id.tv_cancel);
        recyclerView = findViewById(R.id.recyclerView);
        tvTitle.setText(title);

        couponAdapter = new CouponAdapter(mContext);
        couponAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.tv_btn) {
                onButtonClickListener.btnClick(list.get(position).getSh_coupon_code());
            }
        });
        View headView = View.inflate(mContext, R.layout.item_coupon_head, null);
        //赠品列表
        LinearLayout viewPresent = headView.findViewById(R.id.view_present);
        LinearLayout ll_shop_present_container = headView.findViewById(R.id.ll_coupon_present_container);
        TextView tvDesc = headView.findViewById(R.id.tv_desc);
        tvDesc.setText(desc);
        if (promotionList != null && promotionList.size()>0){
            viewPresent.setVisibility(View.VISIBLE);
            ll_shop_present_container.setVisibility(View.VISIBLE);
            ll_shop_present_container.removeAllViews();
            for (PromotionBean promotionBean:promotionList) {
                View view_present_goods = View.inflate(mContext, R.layout.item_present, null);
                ImageView ivPresentGood = view_present_goods.findViewById(R.id.iv_good);
                TextView tvTag = view_present_goods.findViewById(R.id.tv_tag);
                TextView tvPresentTitle = view_present_goods.findViewById(R.id.tv_title);
                TextView tvPresentSkus = view_present_goods.findViewById(R.id.tv_skus);
                TextView tvPresentPrice = view_present_goods.findViewById(R.id.tv_price);
                TextView tvPresentNum = view_present_goods.findViewById(R.id.tv_num);
                view_present_goods.setOnClickListener(view1 -> {
                    if (promotionBean.shType != null) {
                        CommonUtil.goAnyWhere(mContext, promotionBean.shType, promotionBean.shTargetId, "", "", "");
                    }
                });

                GlideUtil.displayRoundImage(mContext,promotionBean.shProductImg,ivPresentGood,7,R.mipmap.defaults);
                tvTag.setVisibility(View.GONE);
                tvPresentTitle.setText(promotionBean.shName);
                String skuStr = "";
                for (String sku : promotionBean.shSku) {
                    skuStr = skuStr + sku;
                }
                tvPresentSkus.setText(skuStr);
                tvPresentPrice.setVisibility(View.GONE);
                tvPresentNum.setText("x"+promotionBean.shQty);
                ll_shop_present_container.addView(view_present_goods);
            }
        }else {
            viewPresent.setVisibility(View.GONE);
            ll_shop_present_container.setVisibility(View.GONE);
        }

        couponAdapter.addHeaderView(headView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(couponAdapter);
        couponAdapter.setNewData(list);

        tvCancel.setOnClickListener(view -> dismiss());
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setWindowAnimations(R.style.bottomShow);
        WindowManager.LayoutParams windowparams = window.getAttributes();
        window.setGravity(Gravity.BOTTOM);
        windowparams.height = (int) (ScreenUtils.getScreenHeight() * 0.58);
        windowparams.width = ScreenUtils.getScreenWidth();
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.setAttributes(windowparams);
    }


    public interface OnButtonClickListener {
        void btnClick(String code);
    }

    private OnButtonClickListener onButtonClickListener;

    public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
        this.onButtonClickListener = onButtonClickListener;
    }
}
